<template>
  <div style="padding: 6px">
    <el-form ref="searchForm" label-width="100px">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-form-item label="订单号">
            <el-input v-model="sd.orderno" size="small" placeholder="订单号" />
          </el-form-item>
        </el-col>
        <el-col :span="3">
          <el-button class="mt_5" type="primary" size="mini" icon="el-icon-search" @click="searchGiftOrder">查询</el-button>
          <el-button class="mt_5" type="primary" size="mini" icon="el-icon-plus" @click="addGiftOrder">添加</el-button>
        </el-col>
      </el-row>
    </el-form>

    <el-table :data="giftOrderObj.list" border style="width: 100%">
      <el-table-column type="expand" width="50">
        <template v-slot="props">
          <div style="width: 99%;margin: 10px auto">
            <el-divider content-position="left">订单描述</el-divider>
            <div v-html="props.row.descption"></div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="订单号" prop="orderNo"></el-table-column>
      <el-table-column label="订单数量" prop="shuliang"></el-table-column>
      <el-table-column label="需求时间">
        <template v-slot="scope">
          <DateFormat :value="scope.row.requireDate" :format="'YYYY-MM-DD'"></DateFormat>
        </template>
      </el-table-column>
      <el-table-column label="订单状态">
        <template v-slot="scope">
          <el-tag type="success" v-if="scope.row.statusId===GiftOrderStatus.shenHePass">{{scope.row.statusname}}</el-tag>
          <el-tag type="danger" v-else-if="scope.row.statusId===GiftOrderStatus.shenHeFail">{{scope.row.statusname}}</el-tag>
          <el-tag v-else>{{scope.row.statusname}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="提单时间">
        <template v-slot="scope">
          <DateFormat :value="scope.row.createTime" :format="'YYYY-MM-DD HH:mm'"></DateFormat>
        </template>
      </el-table-column>
      <el-table-column label="审核人" prop="shenheusername"></el-table-column>
      <el-table-column label="审核时间">
        <template v-slot="scope">
          <DateFormat :value="scope.row.shenheTime" :format="'YYYY-MM-DD HH:mm'" v-if="scope.row.shenheTime"></DateFormat>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="250">
        <template v-slot="scope">
          <div v-show="scope.row.statusId === GiftOrderStatus.add">
            <el-button size="mini" type="primary" @click="editGiftOrder(scope.row.id)">修改</el-button>
            <el-button size="mini" type="warning" @click="submitGiftOrder(scope.row.id)">提交</el-button>
            <el-popconfirm confirmButtonText='确定' cancelButtonText='取消' icon="el-icon-info" iconColor="red"
                           title="确定删除订单数据吗?" @confirm="deleteGiftOrder(scope.row.id)">
              <el-button style="margin-left: 10px" slot="reference" size="mini" type="danger">删除</el-button>
            </el-popconfirm>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination v-if="giftOrderObj.total>sd.pageSize" class="center" background layout="total, prev, pager, next"
                   @current-change="loadGiftOrder" :total="giftOrderObj.total"
                   :current-page.sync="sd.pageNum" :page-size="sd.pageSize">
    </el-pagination>
    <el-dialog :title="popupTitle" :visible.sync="entityDetailVisible" :close-on-click-modal="false" width="75%">
      <TsOrderDetail v-if="entityDetailVisible" :visible.sync="entityDetailVisible" :entityId="popupEntityId"></TsOrderDetail>
    </el-dialog>
  </div>
</template>

<script>
import request from "@/utils/request";
import TsOrderDetail from "./tsOrderDetail";
import GiftOrderStatus from "@/utils/GiftOrderStatus";
import {baseURL} from '@/config';
export default {
  components:{TsOrderDetail},
  data() {
    return {
      GiftOrderStatus: GiftOrderStatus,
      baseUrl: baseURL,
      sd: {
        orderno:"",
        giftName:"",
        pageNum: 1,
        pageSize: 10
      },
      giftOrderObj: {},
      entityDetailVisible : false,
      popupTitle : '',
      popupEntityId : 0,
    }
  },
  mounted() {
    this.loadGiftOrder()
    this.$EventBus.$on("orderadd", () => {
      this.sd.pageNum = 1;
      this.loadGiftOrder();
    });
    this.$EventBus.$on("orderedit", () => {
      this.loadGiftOrder();
    });
  },
  beforeDestroy() {
    this.$EventBus.$off("orderadd")
    this.$EventBus.$off("orderedit")
  },
  methods: {
    searchGiftOrder(){
      this.sd.pageNum = 1;
      this.loadGiftOrder();
    },
    loadGiftOrder() {
      request({
        url: '/gift/order/listtsgiftorder',
        data: this.sd
      }).then(res => {
        const { data } = res
        this.giftOrderObj = data
        this.giftOrderObj.list.forEach((g)=>{
          if(g.filePath){
            g.fileList = [baseURL + g.filePath]
          }
        })
      })
    },
    addGiftOrder(){
      this.popupEntityId = 0;
      this.popupTitle= "新增礼品订单"
      this.entityDetailVisible= true
    },
    editGiftOrder(id){
      this.popupEntityId= id;
      this.popupTitle= "修改礼品订单"
      this.entityDetailVisible= true
    },
    submitGiftOrder(id){
      request({
        url: '/gift/order/submitgiftorder',
        data: {id : id}
      }).then(res => {
        if(res.code === '200') {
          this.$message.success(res.msg)
          this.loadGiftOrder();
        }
      })
    },
    deleteGiftOrder(id){
      request({
        url: '/gift/order/deletegiftorder',
        data: {id : id}
      }).then(res => {
        if(res.code === '200') {
          this.$message.success(res.msg)
          this.loadGiftOrder();
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
